package com.orkasgb.framework.controller;

import com.orkasgb.framework.orkasgbframework.spring.springsecurity.CommonResult;
import com.orkasgb.framework.orkasgbframework.spring.springsecurity.sms.OrkasgbSMSAuthenticationProvider;
import com.orkasgb.framework.orkasgbframework.spring.springsecurity.sms.OrkasgbSMSAuthenticationToken;
import jakarta.websocket.server.PathParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/apiTest")
public class ApiTestController {

	@PreAuthorize("hasAnyAuthority('ADMIN')")
	@RequestMapping("/getApiData")
	@ResponseBody
	public CommonResult<Map<String, String>> getApiData(@RequestBody Map<String, String> param) {
		return CommonResult.ofSuccess(param, "请求成功！");
	}

	@RequestMapping("/getApiData1/{name}")
	@ResponseBody
	public CommonResult<String> getApiData1(@PathVariable(name = "name1", value = "name") String name) throws UnsupportedEncodingException {
		return CommonResult.ofSuccess(name, "请求成功！");
	}

	@PreAuthorize("hasAnyAuthority('ADMIN2')")
	@RequestMapping("/getApiData2")
	@ResponseBody
	public CommonResult<List> getApiData1() throws UnsupportedEncodingException {
		return CommonResult.ofSuccess(List.of(), "请求成功！");
	}

	@RequestMapping("/login")
	@ResponseBody
	public CommonResult<List> login(@PathParam("loginType") String loginType) throws UnsupportedEncodingException {
		ProviderManager providerManager = new ProviderManager();
		if (StringUtils.equals(loginType, "SMS")) {
			OrkasgbSMSAuthenticationProvider orkasgbSMSAuthenticationProvider = new OrkasgbSMSAuthenticationProvider();
			providerManager = new ProviderManager(orkasgbSMSAuthenticationProvider);
		}
		Authentication authenticate = providerManager.authenticate(new OrkasgbSMSAuthenticationToken("orkasgb", "orkasgb"));
		return CommonResult.ofSuccess(List.of(authenticate), "请求成功！");
	}
}
